package com.heakey.common.exception.handler;

import com.heakey.common.exception.BusinessException;
import com.heakey.common.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description 业务异常处理器
 * @Author Bing
 * @Date 2024/10/14 18:15
 */
@Slf4j
@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE) // 加上这个注解让BusinessExceptionHandler优先级高于  GlobalExceptionHandler
public class BusinessExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public Result handleServiceException(BusinessException e, HttpServletRequest request)
    {
        log.error(e.getMessage(), e);
        return Result.error(e.getCode(), e.getMessage());
    }

}